//
//  LockController.m
//  新华易贷
//
//  Created by 吴 凯 on 15/9/21.
//  Copyright (c) 2015年 吴 凯. All rights reserved.
//

#import "LockController.h"
#import "LockView.h"
#import "UIView+Extension.h"
#import "Masonry.h"
#define KSCREENW [UIScreen mainScreen].bounds.size.width

#define KSCREENH [UIScreen mainScreen].bounds.size.height

@interface LockController ()<LockViewDelegate,UIGestureRecognizerDelegate>
//@property (weak, nonatomic) IBOutlet UILabel *locklabel;
@property(nonatomic,weak)UILabel *unlocklabel;
@property(nonatomic,weak)UILabel *setpassword;
//标记是否是重置密码
@property(nonatomic ,assign,getter=isresetpassword)BOOL resetpassword;
//判断是否是两次确认密码
@property(nonatomic,assign,getter=istwopassword)BOOL twopassword;
@property(weak,nonatomic)LockView *lockview;
@property(nonatomic,strong)UIImageView *lockImageView;
@property (nonatomic,strong) UIView *backgroundViews;
@property (nonatomic,strong) UIView *bigView;
@property (nonatomic,strong) UIView *smailView;
@end

@implementation LockController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    if (self.tagsStyle==0)
    {
         self.navigationItem.title=@"取消手势密码";
        
    }else if (self.tagsStyle==1){
        
         self.navigationItem.title=@"修改手势密码";
        
    }else{
        
         self.navigationItem.title=@"设置手势密码";
        
    }
    
}
-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    self.backgroundViews=[[UIView alloc] init];
    self.backgroundViews.backgroundColor=[UIColor whiteColor];;
    [self.view addSubview:self.backgroundViews];
    [self.backgroundViews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(kNavbarHeight);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight));
        
    }];
    [self.view addSubview:self.backgroundViews];
    self.lockImageView=[[UIImageView alloc] init];
    self.lockImageView.frame=CGRectMake(KSCREENW/2-12, 34, 24, 26);
    self.lockImageView.image=[UIImage imageNamed:@"icon_suo"];
    [self.backgroundViews addSubview:self.lockImageView];
    self.resetpassword = NO;
    self.twopassword = NO;
    LockView *lockview = [[LockView alloc]init];
    lockview.backgroundColor = [UIColor clearColor];
    lockview.width = 300;
    lockview.height = 300;
    lockview.x = (KSCREENW - lockview.width) * 0.5;
    lockview.y = 150;
    self.lockview = lockview;
    lockview.delegate = self;
    [self.backgroundViews addSubview:lockview];
    self.view.backgroundColor=[UIColor whiteColor];
    //原来的
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"HomeButtomBG"]];
    self.lockview.delegate = self;
    [self judgeMentLocalPassWord];
   }

//设置状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
}

//添加重置密码按钮
- (void)addResetPassWordBuutton{
    UIButton *resetpassword = [[UIButton alloc]init];
    [resetpassword setTitle:@"重置密码" forState:UIControlStateNormal];
    [resetpassword setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [resetpassword sizeToFit];
    resetpassword.y = CGRectGetMaxY(self.lockview.frame) + 50;
    resetpassword.x = (KSCREENW - resetpassword.width) * 0.5;
    [self.backgroundViews addSubview:resetpassword];
    [resetpassword addTarget:self action:@selector(resetPassWord) forControlEvents:UIControlEventTouchUpInside];
    
}
//添加下次设置按钮
- (void)addCancelButton{
    UIButton *cancelbutt = [[UIButton alloc]init];
    [cancelbutt setTitle:@"下次设置" forState:UIControlStateNormal];
    [cancelbutt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [cancelbutt sizeToFit];
    cancelbutt.y = CGRectGetMaxY(self.lockview.frame) + 50;
    cancelbutt.x = (KSCREENW - cancelbutt.width) * 0.5;
    [self.backgroundViews addSubview:cancelbutt];
    [cancelbutt addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];

}

//取消设置按钮
- (void)cancelButtonClick{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

//重置按钮点击事件
- (void)resetPassWord{
    self.resetpassword = YES;
    [self setLocklabel:@"确认旧手势密码"];
    [self.unlocklabel sizeToFit];
    self.unlocklabel.x = (KSCREENW - self.unlocklabel.width) * 0.5;
    self.unlocklabel.y = CGRectGetMinY(self.lockview.frame) - 75;

}

//lockview的代理方法
- (BOOL)unlockView:(LockView *)unlockView withPassword:(NSString *)password
{
    NSString *localpasswordone = [[NSUserDefaults standardUserDefaults]objectForKey:@"passwordone"];
    NSString *localpasswordtwo = [[NSUserDefaults standardUserDefaults]objectForKey:@"passwordtwo"];
    
    if (self.twopassword) {
        if ([localpasswordone isEqualToString:localpasswordtwo]) {
//            UIAlertView *confirmalertview = [[UIAlertView alloc]initWithTitle:@"密码设置成功" message:@"请输入密码解锁" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
//            [confirmalertview show];
//            [self setLocklabel:@"手势解锁"];
            //成功传1
            [self creatsmallView:1];
            
            self.twopassword = NO;
            return YES;
       }
       
        else
        {
//            UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"与上次密码不对应" message:@"请重新设置密码" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
//            [alertview show];
            //两次不一样的传0
            [self creatsmallView:0];
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordone"];
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordtwo"];
            [self setLocklabel:@"请重新绘制手势密码"];
            return YES;
        }
    }
    
    else
        
    {
            if ([password isEqualToString:localpasswordone]) {
                if (self.isresetpassword) {
                    
                    if (self.tagsStyle==0)
                    {
                        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordone"];
                        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordtwo"];
                        [[NSUserDefaults standardUserDefaults]setObject:@"NO" forKey:[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]];
                        //暂时写12
//                        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"phoneNumber"];
                        [self.navigationController popViewControllerAnimated:YES];
                        self.resetpassword = NO;
                    }else if (self.tagsStyle==1){
                    
                    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordone"];
                    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordtwo"];
                        
                    //暂时写12
//                    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"phoneNumber"];
                    [self setLocklabel:@"为了您的账户安全,请设置手势密码"];
                    self.resetpassword = NO;
                    }
                }
                else
                {
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }
        return YES;
        }
        else {
                return NO;
        }
        return NO;
    }
}
-(void)creatsmallView:(NSInteger)num{
    
    //弹出提示框
    self.bigView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.bigView.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.3];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backks:)];
    tap.delegate=self;
    [self.bigView addGestureRecognizer:tap];
    
    self.smailView=[[UIView alloc] initWithFrame:CGRectMake(35, [UIScreen mainScreen].bounds.size.height/2-90, [UIScreen mainScreen].bounds.size.width-70, 180)];
    self.smailView.backgroundColor=[UIColor whiteColor];
    self.smailView.layer.cornerRadius=4;
    self.smailView.userInteractionEnabled=YES;
    
    UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(0, 129, self.smailView.frame.size.width, 1)];
    lineView.backgroundColor=[UIColor grayColor];
    lineView.alpha=0.1;
    [self.smailView addSubview:lineView];
    
    UIImageView *iage=[[UIImageView alloc] init];
    if (num==0)
    {
       iage.image=[UIImage imageNamed:@"icon_byz"];
    }else{
        
        iage.image=[UIImage imageNamed:@"ICON_OK"];
    }
    
    [self.smailView addSubview:iage];
    CGFloat CX=self.smailView.frame.size.width/2-25;
    [iage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.smailView.mas_top).offset(13);
        make.left.equalTo(self.smailView.mas_left).offset(CX);
    }];
    
    
    UILabel *smalLab=[[UILabel alloc] initWithFrame:CGRectMake(0, 55, self.smailView.frame.size.width, 20)];
    if (num==0)
    {
        smalLab.text=@"两次手势输入不一致";
    }else{
        
        smalLab.text=@"手势密码设置成功";
        
    }
    
    smalLab.textAlignment = NSTextAlignmentCenter;
    [self.smailView addSubview:smalLab];
    
    [smalLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iage.mas_bottom).offset(20);
        make.left.equalTo(self.smailView.mas_left);
        make.right.equalTo(self.smailView.mas_right);
        make.height.equalTo(@(20));
    }];
    
    UIButton *knowBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [knowBtn setTitle:@"知道了" forState:UIControlStateNormal];
    [knowBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    if (num==0)
    {
       [knowBtn addTarget:self action:@selector(gBackss) forControlEvents:UIControlEventTouchUpInside];
    }else{
        
       [knowBtn addTarget:self action:@selector(gBacksOK) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    [self.smailView addSubview:knowBtn];
    
    [knowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(lineView.mas_top).offset(2);
        make.left.equalTo(self.smailView.mas_left);
        make.right.equalTo(self.smailView.mas_right);
        make.bottom.equalTo(self.smailView.mas_bottom).offset(-2);
        
    }];
    
    
    [self.bigView addSubview:self.smailView];
    //加到全屏
    [[UIApplication sharedApplication].keyWindow addSubview:self.bigView];
    
    
}
//手势密码设置成功返回上一个页面
-(void)gBacksOK{
    
    self.bigView.hidden=YES;
    //根据用户名去存取是否设置手势密码成功
    NSString *userName=[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];//暂时写死
    [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:userName];
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)backks:(UIGestureRecognizer *)tp{
    
    self.bigView.hidden=YES;
    
}
-(void)gBackss{
    
    self.bigView.hidden=YES;
    
}
//手势的代理(点击大的视图，防止触发小的视图)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:self.smailView]) {
        return NO;
    }
    return YES;
}
- (void)setPassWordSuccess:(NSString *)tabelname{
    NSString *localpasswordone = [[NSUserDefaults standardUserDefaults]objectForKey:@"passwordone"];
    NSString *localpasswordtwo = [[NSUserDefaults standardUserDefaults]objectForKey:@"passwordywo"];
    if (!localpasswordtwo||!localpasswordone ) {
        self.twopassword = YES;
    }
    self.lockImageView.image=[UIImage imageNamed:@"icon_queren"];
    self.setpassword.text = tabelname;
    self.unlocklabel.text = tabelname;
    [self.unlocklabel sizeToFit];
    [self.setpassword sizeToFit];
    self.setpassword.x = (KSCREENW - self.setpassword.width) * 0.5;
    self.setpassword.y = CGRectGetMinY(self.lockview.frame) -75;
    self.unlocklabel.x = (KSCREENW - self.unlocklabel.width) * 0.5;
    self.unlocklabel.y = CGRectGetMinY(self.lockview.frame) -75;

}

//判断本地是否存有密码
- (void)judgeMentLocalPassWord{
    
    NSString *password = [[NSUserDefaults standardUserDefaults]objectForKey:@"passwordone"];
//    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordone"];
//    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"passwordtwo"];
    if (password == nil) {
        //添加下次设置按钮
//        [self addCancelButton];
        [self setPassWordView:@"为了您的账户安全,请设置手势密码"];
    }else{
        //添加重置密码按钮
//        [self addResetPassWordBuutton];
        [self unlockView:@"手势解锁"];
        
        if (self.tagsStyle==0)//取消手势密码
        {
            [self resetPassWord];
        }else if (self.tagsStyle==1)//修改手势密码
        {
            
           [self resetPassWord];
            
        }else{//第一次进入手势密码设置页面
            
        }
        
    }

}

//设置密码界面
- (void)setPassWordView:(NSString *)lockstr{
    UILabel *locklabel = [[UILabel alloc]init];
    locklabel.text = lockstr;
    locklabel.textAlignment = NSTextAlignmentCenter;
    self.setpassword = locklabel;
    locklabel.textColor = [UIColor blueColor];
    [locklabel sizeToFit];
    locklabel.x = (KSCREENW - locklabel.width) * 0.5;
    locklabel.y = CGRectGetMinY(self.lockview.frame) -75;
    
    [self.backgroundViews addSubview:locklabel];
    }

- (void)setLocklabel:(NSString *)locklstr{
    
    self.setpassword.text = locklstr;
    self.unlocklabel.text = locklstr;
    [self.setpassword sizeToFit];
    [self.unlocklabel sizeToFit];
    if (self.tagsStyle==2)
    {
       
        self.setpassword.x = (KSCREENW - self.setpassword.width) * 0.5;
        self.setpassword.y = CGRectGetMinY(self.setpassword.frame) +5;
        self.unlocklabel.x = (KSCREENW - self.unlocklabel.width) * 0.5;
        self.unlocklabel.y = CGRectGetMinY(self.lockview.frame) +5;
    }else{
        self.setpassword.x = (KSCREENW - self.setpassword.width) * 0.5;
        self.setpassword.y = CGRectGetMinY(self.setpassword.frame) -75;
        self.unlocklabel.x = (KSCREENW - self.unlocklabel.width) * 0.5;
        self.unlocklabel.y = CGRectGetMinY(self.lockview.frame)-75;
        
}
    self.setpassword.textColor = [UIColor blueColor];
    self.unlocklabel.textColor = [UIColor blueColor];
    
    [self.backgroundViews addSubview:self.setpassword];
    [self.backgroundViews addSubview:self.unlocklabel];

}

//手势解锁界面
- (void)unlockView:(NSString *)unlockstr{
    UILabel *locklabel = [[UILabel alloc]init];
    self.unlocklabel = locklabel;
    locklabel.text = unlockstr;
    locklabel.textColor = [UIColor whiteColor];
    [locklabel sizeToFit];
    locklabel.x = (KSCREENW - locklabel.width) * 0.5;
    locklabel.y = CGRectGetMinY(self.lockview.frame)-75;
    [self.backgroundViews addSubview:locklabel];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
